package com.itheima.test7;

import java.util.ArrayList;
import java.util.Collections;
import java.util.StringJoiner;
import java.util.concurrent.Callable;

public class MyCollable implements Callable<Integer> {
    ArrayList<Integer> list;

    public MyCollable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> boxlist = new ArrayList<>();
        //1.循环
        while (true) {
            //2.同步代码块
            synchronized (com.itheima.test5.MyThread.class) {
                //3.判断共享资源（已经到末尾）
                if (list.size() == 0) {
                    getInfo(boxlist, Thread.currentThread().getName());
                    break;
                } else {
                    //4.判断共享资源（没有到末尾）
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    boxlist.add(prize);
                }
            }
            Thread.sleep(10);
        }
        if(boxlist.size()==0){
            return null;
        }
        return Collections.max(boxlist);
    }


    public void getInfo(ArrayList<Integer> list, String name) {
        StringJoiner sj = new StringJoiner(",", "", "");
        int max = Collections.max(list);
        int sum = 0;
        for (Integer i : list) {
            sum += i;
            sj.add(i + "");
        }
        System.out.println("在此次抽奖过程中，" + name + "总共产生了6个奖项。分别为：" + sj.toString() + "最高奖项为" + max + "元，总计额为" + sum + "元");
    }


}
